<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Asynchronous Operations</title>
    <link rel="stylesheet" href="../../../boostbook.css" type="text/css">
    <meta name="generator" content="DocBook XSL Stylesheets V1.75.2">
    <link rel="home" href="../../../index.html" title="Asio">
    <link rel="up" href="../model.html" title="Asynchronous Model">
    <link rel="prev" href="../model.html" title="Asynchronous Model">
    <link rel="next" href="async_agents.html" title="Asynchronous Agents">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<table cellpadding="2" width="100%">
    <tr>
        <td valign="top"><img alt="asio C++ library" width="250" height="60" src="../../../asio.png"></td>
    </tr>
</table>
<hr>
<div class="spirit-nav">
    <a accesskey="p" href="../model.html"><img src="../../../prev.png" alt="Prev"></a><a accesskey="u"
                                                                                         href="../model.html"><img
        src="../../../up.png" alt="Up"></a><a accesskey="h" href="../../../index.html"><img src="../../../home.png"
                                                                                            alt="Home"></a><a
        accesskey="n" href="async_agents.html"><img src="../../../next.png" alt="Next"></a>
</div>
<div class="section">
    <div class="titlepage">
        <div>
            <div><h4 class="title">
                <a name="asio.overview.model.async_ops"></a><a class="link" href="async_ops.html"
                                                               title="Asynchronous Operations">Asynchronous
                Operations</a>
            </h4></div>
        </div>
    </div>
    <p>
        <span class="inlinemediaobject"><img src="../../../async_op_model.png" width="421"></span>
    </p>
    <p>
        An <span class="emphasis"><em>asynchronous operation</em></span> is the basic unit of composition
        in the Asio asynchronous model. Asynchronous operations represent work
        that is launched and performed in the background, while the user's code
        that initiated the work can continue with other things.
    </p>
    <p>
        Conceptually, the lifecycle of an asynchronous operation can be described
        in terms of the following events and phases:
    </p>
    <p>
        <span class="inlinemediaobject"><img src="../../../async_op_phases.png" width="861"></span>
    </p>
    <p>
        An <span class="emphasis"><em>initiating function</em></span> is a function which may be
        called by the user to start an asynchronous operation.
    </p>
    <p>
        A <span class="emphasis"><em>completion handler</em></span> is a user-provided, move-only
        function object that will be invoked, at most once, with the result of
        the asynchronous operation. The invocation of the completion handler tells
        the user about something that has already happened: the operation completed,
        and the side effects of the operation were established.
    </p>
    <p>
        The initiating function and completion handler are incorporated into the
        user's code as follows:
    </p>
    <p>
        <span class="inlinemediaobject"><img src="../../../async_op_init_complete.png" width="496"></span>
    </p>
    <p>
        Synchronous operations, being embodied as single functions, have several
        inherent semantic properties as a consequence. Asynchronous operations
        adopt some of these semantic properties from their synchronous counterparts,
        in order to facilitate flexible and efficient composition.
    </p>
    <div class="informaltable">
        <table class="table">
            <colgroup>
                <col>
                <col>
            </colgroup>
            <thead>
            <tr>
                <th>
                    <p>
                        Property of synchronous operations
                    </p>
                </th>
                <th>
                    <p>
                        Equivalent property of asynchronous operations
                    </p>
                </th>
            </tr>
            </thead>
            <tbody>
            <tr>
                <td>
                    <p>
                        When a synchronous operation is generic (i.e. a template) the
                        return type is deterministically derived from the function and
                        its arguments.
                    </p>
                </td>
                <td>
                    <p>
                        When an asynchronous operation is generic, the completion handler's
                        arguments' types and order are deterministically derived from
                        the initiating function and its arguments.
                    </p>
                </td>
            </tr>
            <tr>
                <td>
                    <p>
                        If a synchronous operation requires a temporary resource (such
                        as memory, a file descriptor, or a thread), this resource is
                        released before returning from the function.
                    </p>
                </td>
                <td>
                    <p>
                        If an asynchronous operation requires a temporary resource (such
                        as memory, a file descriptor, or a thread), this resource is
                        released before calling the completion handler.
                    </p>
                </td>
            </tr>
            </tbody>
        </table>
    </div>
    <p>
        The latter is an important property of asynchronous operations, in that
        it allows a completion handler to initiate further asynchronous operations
        without overlapping resource usage. Consider the trivial (and relatively
        common) case of the same operation being repeated over and over in a chain:
    </p>
    <p>
        <span class="inlinemediaobject"><img src="../../../async_op_trivial_chain.png" width="375"></span>
    </p>
    <p>
        By ensuring that resources are released before the completion handler runs,
        we avoid doubling the peak resource usage of the chain of operations.
    </p>
</div>
<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%">
    <tr>
        <td align="left"></td>
        <td align="right">
            <div class="copyright-footer">Copyright © 2003-2022 Christopher M. Kohlhoff<p>
                Distributed under the Boost Software License, Version 1.0. (See accompanying
                file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>)
            </p>
            </div>
        </td>
    </tr>
</table>
<hr>
<div class="spirit-nav">
    <a accesskey="p" href="../model.html"><img src="../../../prev.png" alt="Prev"></a><a accesskey="u"
                                                                                         href="../model.html"><img
        src="../../../up.png" alt="Up"></a><a accesskey="h" href="../../../index.html"><img src="../../../home.png"
                                                                                            alt="Home"></a><a
        accesskey="n" href="async_agents.html"><img src="../../../next.png" alt="Next"></a>
</div>
</body>
</html>
